"""
Case Type   : GUC
Case Name   : 使用alter system set方式修改max_active_global_temporary_table为10
Create At   : 2021/3/15
Owner       : opentestcase001
Description :
    1、修改max_active_global_temporary_table为10
    2、查询
    3、创建全局临时表
    4、清理环境
Expect      :
    1、设置成功
    2、结果为10
    3、成功
    4、成功
History     :modified by opentestcase005,2022.09.13，参数类型变更，修改参数设置方式
             modified by wangtianjing,2024.7.2，同步sharestorage目录下优化后的用例
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class GucTestCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.comsh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.table = 't_globaltemptab0002'
        self.default_value = self.common.show_param(
            'max_active_global_temporary_table')

    def test_guc(self):
        text = '--step1:修改max_active_global_temporary_table为10;expect:设置成功'
        self.log.info(text)
        alter_cmd = self.comsh.execut_db_sql(
            'alter system set max_active_global_temporary_table=10;')
        self.assertTrue(alter_cmd)
        self.comsh.restart_db_cluster()
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Normal" in status or 'Degraded' in status,
                        '执行失败' + text)

        text = '--step2:查询;expect:结果为10--'
        self.log.info(text)
        sql_cmd = self.common.show_param('max_active_global_temporary_table')
        self.log.info(sql_cmd)
        self.assertEqual('10', sql_cmd, '执行失败' + text)

        text = '--step3:创建全局临时表;expect:成功--'
        self.log.info(text)
        sql_cmd = f'''drop table if exists {self.table};
            create global temp table {self.table}(c_int int);
            insert into {self.table} values(1),(2);
            update {self.table} set c_int = 5 where c_int = 1;
            delete from {self.table} where c_int = 2;
            select * from {self.table};
            '''
        result = self.comsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertNotIn(self.constant.SQL_WRONG_MSG[1], result, '执行失败' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, result,
                      '执行失败' + text)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG, result,
                      '执行失败' + text)

    def tearDown(self):
        text = '--step4:清理环境;expect:成功--'
        self.log.info(text)
        clear_cmd = self.comsh.execut_db_sql(
            f'drop table if exists {self.table};')
        self.log.info(clear_cmd)
        alter_cmd = self.comsh.execut_db_sql(
            f'alter system set max_active_global_temporary_table='
            f'{self.default_value};')
        self.log.info(alter_cmd)
        self.comsh.restart_db_cluster()
        status = self.comsh.get_db_cluster_status()
        self.assertTrue("Normal" in status or 'Degraded' in status,
                        '执行失败' + text)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, clear_cmd)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
